<html>
<head>
    <meta charset="utf-8">
    <title>添加ASMR</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">ASMR ID</label>
        <div class="layui-input-block">
            <input type="text" id="id" name="id" lay-verify="required" lay-reqtext="ASMR的ID不能为空"
                   placeholder="请输入ASMR的ID" value="" class="layui-input">
            <tip>填写ASMR的ID。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">ASMR名称</label>
        <div class="layui-input-block">
            <input type="text" id="title" name="title" lay-verify="required" lay-reqtext="ASMR名称不能为空"
                   placeholder="请输入ASMR名称" value="" class="layui-input">
            <tip>填写ASMR的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">社团名称</label>
        <div class="layui-input-block">
            <input id="name" name="name" type="text" class="layui-input"
                   placeholder="社团名称"></input>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">cv名字</label>
        <div class="layui-input-block">
            <input id="cv" name="cv" type="text" class="layui-input"
                   placeholder="请输入cv名字"></input>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">ASMR图片地址</label>
        <div class="layui-input-block">
            <input id="img" name="img" class="layui-input" placeholder="请输入ASMR的图片地址"></input>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">ASMR小图片地址</label>
        <div class="layui-input-block">
            <input id="img240" name="img240" class="layui-input" placeholder="请输入ASMR小图片地址"></input>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label ">ASMR评分人数</label>
        <div class="layui-input-block">
            <input id="rateCount" name="rateCount" type="text" class="layui-input"
                   placeholder="请输入ASMR评分人数"></input>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">ASMR评分</label>
        <div class="layui-input-block">
            <input id="rateAverage" name="rateAverage" type="text" class="layui-input"
                   placeholder="请输入ASMR评分"></input>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">发布日期</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" id="createDate">
            <!--            <input id="createData" name="createData" type="text" class="layui-input"-->
            <!--                   placeholder="发布日期"></input>-->
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-block" id="tagid">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        const form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            $ = layui.$;

        $.ajaxSetup({
            beforeSend: function (xhr) {
// 假设你的 token 存储在 localStorage 中
                const token = localStorage.getItem('token');
                if (token) {
// 将 token 添加到请求头中
                    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
                }
            }
        });
        const queryString = window.location.search;
        const queryParams = new URLSearchParams(queryString);
        const asmrId = queryParams.get('id');
        let tDate;

        function bindselect(id, url) {
            $.ajax({
                url: url,//访问的链接
                type: 'get',//提交方式 常用的是get或者post
                dataType: "json",//返回的数据类型
                async: false,//是否异步加载
                success: function (res) {//访问成功返回的方法
                    const data = res;
                    let html = '';
                    for (const row of data) {
                        html += '<input type="checkbox" name="' + row['id'] + '"  title="' + row['name'] + '" value="' + row['id'] + '"  lay-skin="primary">';
                    }
                    $("#" + id).html(html);
                    form.render(); //更新全部
                    if (asmrId) {//编辑
                        //通过id获取数据
                        $.ajax({
                            url: "/api/admin/asmr?action=get&id=" + asmrId,//访问的链接
                            type: 'get',//提交方式 常用的是get或者post
                            dataType: "json",//返回的数据类型
                            // async: false,//是否异步加载
                            success: function (res) {//访问成功返回的方法
                                if (res.success) {
                                    $("#id").val(res.data.id)//设置id为name的数据框的value属性为xxxx
                                    $("#id").attr('disabled', true)//编辑不可以改动id
                                    $("#title").val(res.data.title)
                                    $("#img").val(res.data.img)
                                    $("#img240").val(res.data.img240)
                                    $("#name").val(res.data.name)
                                    $("#rateCount").val(res.data.rateCount)
                                    $("#rateAverage").val(res.data.rateAverage)
                                    $("#cv").val(res.data.cv)
                                    // bindselect("tagid", "/api/asmr/tags", "id", "name")
                                    const tags = res.data.tags
                                    $("#" + id).find("input").each(function () {
                                        let value = parseInt($(this).val());
                                        if (tags.includes(value)) {
                                            $("input:checkbox[value=" + value + "]").attr("checked", true);
                                        }
                                    })
                                    tDate = res.data.createDate;
                                    laydate.render({
                                        elem: '#createDate',//指定元素
                                        value: tDate,
                                        isInitValue: true,
                                    });
                                    console.log(laydate.value)
                                    form.render(); //更新全部
                                }
                            },
                            error: function (err) {//访问失败返回的方法
                                //  debugger
                            }
                        })
                    } else {
                        tDate = new Date();
                        laydate.render({
                            elem: '#createDate',//指定元素
                            value: tDate,
                        });
                    }
                },
                error: function (err) {//访问失败返回的方法
                    //  debugger
                }
            })
        }

        bindselect('tagid', "/api/asmr/tags")


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            const checkedValues = $("#tagid").find("input[type='checkbox']:checked").map(function () {
                return $(this).val();
            }).get().join(",");
            console.log(checkedValues)
            let url
            tDate = $("#createDate").val()
            if (asmrId) {//编辑
                url = "/api/admin/asmr?action=edit&id=" + data.field.id + "&title=" + data.field.title + "&img=" + data.field.img
                    + "&img240=" + data.field.img240 + "&name=" + data.field.name + "&rateCount=" + data.field.rateCount + "&rateAverage=" + data.field.rateAverage
                    + "&cv=" + data.field.cv + "&createDate=" + tDate + "&tags=" + checkedValues; //编辑
            } else {//添加
                url = "/api/admin/asmr?action=add&id=" + data.field.id + "&title=" + data.field.title + "&img=" + data.field.img
                    + "&img240=" + data.field.img240 + "&name=" + data.field.name + "&rateCount=" + data.field.rateCount + "&rateAverage=" + data.field.rateAverage
                    + "&cv=" + data.field.cv + "&createDate=" + tDate + "&tags=" + checkedValues;
            }

            $.ajax({
                url: url,//访问的链接
                type: 'get',//提交方式 常用的是get或者post
                dataType: "json",//返回的数据类型
                // async: false,//是否异步加载
                success: function (res) {//访问成功返回的方法
                    // debugger
                    layer.msg(res.message);
                    setTimeout(function () {
                        //执行搜索重载
                        parent.layui.table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                        }, 'data');
                        const iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);

                    }, 1000)

                },
                error: function (err) {//访问失败返回的方法
                    //  debugger
                }
            })
            return false;
        });

    });
</script>
</body>
</html>
